package com.winit.deploy.task.tomcatTask;

import com.winit.core.task.Task;
import com.winit.deploy.bean.DeployEnum;
import com.winit.deploy.server.WinitTomcatServer;
import com.winit.deploy.util.DeployUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangjian on 16/8/10.
 * 发布任务step4 安装
 */
public class DeployInstallTask extends Task {

    static final Logger logger = LoggerFactory.getLogger(DeployInstallTask.class);

    private WinitTomcatServer winitTomcatServer;

    public DeployInstallTask(String taskId, Integer expireTime,WinitTomcatServer winitTomcatServer){
        super(taskId,expireTime);
        this.winitTomcatServer = winitTomcatServer;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.tomcatTask.DeployInstallTask.call start taskId="+this.taskId);
        boolean flag = this.winitTomcatServer.doInstall();
        if(flag){
            String projectName = this.winitTomcatServer.getWinitTomcatInfo().getProjectName();
            String editionNum = this.winitTomcatServer.getWinitTomcatInfo().getEditionNum();
            if(this.taskId.startsWith("staging")){
                DeployUtil.updateProjectVersion(projectName,editionNum, DeployEnum.STAGING);
            }
            if(this.taskId.startsWith("online")){
                DeployUtil.updateProjectVersion(projectName,editionNum,DeployEnum.ONLINE);
                DeployUtil.cpOnlineVersionToNas(projectName,editionNum);
            }
        }
        logger.info("com.winit.deploy.task.tomcatTask.DeployInstallTask.call end taskId="+this.taskId);
        return flag?TaskStatus.STATUS_SUCCESS:TaskStatus.STATUS_ERROR;
    }
}
